#!/bin/bash

###############################################
###### IRIS-Processor #########################
###############################################

# This script processes an iris-database the following way
# Input Image 	=> Segmentation result (<filename>.png)
#		=> Iris code	       (<filename>.png)


if [ $# -ne 3 ]; then
	echo 'Call with parameters <SEGMENTATIONTOOL> <DATAROOT> <TARGET>';
	echo '<SEGMENTATIONTOOL> ... wahet or caht <COMPRESSIONTYPE> <DATAROOT>';
	echo '<DATAROOT> ... e.g. iitd_cr60';
	echo '<TARGET> ... e.g. iitd_cr60';
	exit;
fi
 


TOOL=$1
TARGET=$3
DATAROOT=$2

echo 'segmentation of ' $DATAROOT ' with ' $TOOL to $TARGET

CRS=`ls $DATAROOT`


SEG_EXT=png
ITX_EXT=png

DIRNAME_SEG=seg
DIRNAME_TEXTURE=itex
TOOLDIR=bin

for CR in $CRS
do
   # Clean up
    OUTDIR=$TARGET/$TOOL/$CR
    INDIR=$DATAROOT/$CR
  
  
    # OK, for double compressed it might be there are some different qualities for the first compression step
    quals=`ls -d $INDIR/*/`
    #echo $quals;
    
    if [ -z "$quals" ] ; then
      quals=/
    fi
    
    
    for qual in $quals
    do
    
      qual=${qual/$INDIR/}
    
      echo $qual;
     
      
     # continue;
     # exit
      
      rm -rf $OUTDIR/$qual
      mkdir -p $OUTDIR/$qual/$DIRNAME_SEG
      mkdir -p $OUTDIR/$qual/$DIRNAME_TEXTURE

      FILELIST=`ls $INDIR/$qual`

      #echo $FILELIST


      for INPUTFILE in $FILELIST
      do
	extension="${INPUTFILE##*.}"
	y=${INPUTFILE%.$extension}
	FNAME=`echo ${y##*/}`

	echo $TOOL ': Processing ' $FNAME ' to '  $OUTDIR/$qual/$DIRNAME_SEG/$FNAME.$SEG_EXT
	INPUT=$INDIR/$qual/$INPUTFILE
	echo $INPUTFILE
	$TOOLDIR/$TOOL -i $INPUT -bm $OUTDIR/$qual/$DIRNAME_SEG/$FNAME.$SEG_EXT -o $OUTDIR/$qual/$DIRNAME_TEXTURE/$FNAME.$ITX_EXT -s 512 64 -e -q

      done
    done

  done
